﻿class classes.QuizMcq
{
	private var m_arrQuestionsData:Array;
	private var m_mcContainer:MovieClip;
	private var m_intCurrentQuestion:Number;
	private var m_objCurrentQuestion:Object;
	private var m_objCuePointListner:Object;
	private var m_intTotalCorrect:Number;
	private var m_intInterval:Number;
	private var m_isCorrect:Boolean;
	private var m_strCongrVideoPath:String;
	private var m_strMoreInfo:String;


	public function QuizMcq(mcContainer:MovieClip)
	{
		m_mcContainer = mcContainer;
		m_mcContainer.mcTimeOut._visible = false;
		m_intCurrentQuestion = 0;
		m_intTotalCorrect = 0;
		loadXMLFile();
	}

	private function loadXMLFile():Void
	{
		var objThis = this;
		var objSegmentXML:XML = new XML();
		objSegmentXML.ignoreWhite = true;
		objSegmentXML.onLoad = function(s)
		{
			if(s)
			{
				objThis.m_arrQuestionsData = new Array();

				var nLength = objSegmentXML.firstChild.firstChild.childNodes.length;
				objThis.m_strCongrVideoPath  = objSegmentXML.firstChild.childNodes[1].attributes.videoPath;
				trace("objThis.m_strCongrVideoPath  =<><><><><><><><>= " +  objThis.m_strCongrVideoPath );
				objThis.m_strMoreInfo  = objSegmentXML.firstChild.childNodes[2].firstChild.nodeValue;

				_root.objUpFront.moreInfo = objThis.m_strMoreInfo;

				for(var i:Number = 0; i< nLength; i++)
				{
					var objTemp:Object = new Object();
					var xmlNode:XMLNode = objSegmentXML.firstChild.firstChild.childNodes[i];

					// store FLV information
					objTemp.strFlvPath = xmlNode.childNodes[0].childNodes[0].firstChild.nodeValue;
					objTemp.arrCuePoints = xmlNode.childNodes[0].childNodes[1].firstChild.nodeValue.split(",");

					// store Question string
					objTemp.strQuestion = xmlNode.childNodes[1].firstChild.nodeValue;


					// Parse through the options
					objTemp.arrOptions = new Array();
					for(var j:Number = 0; j<xmlNode.childNodes[2].childNodes.length; j++)
					{
						objTemp.arrOptions.push(xmlNode.childNodes[2].childNodes[j].firstChild.nodeValue);

						if(xmlNode.childNodes[2].childNodes[j].attributes.isCorrect == "true")
							objTemp.intCorrectOption = j;
					}

					// store feedbacks
					objTemp.strCorrectFeedback = xmlNode.childNodes[2].childNodes[0].firstChild.nodeValue;
					objTemp.strIncorrectFeedback = xmlNode.childNodes[2].childNodes[1].firstChild.nodeValue;

					objThis.m_arrQuestionsData.push(objTemp);
				}
				//trace("XML Loaded " + m_arrQuestionsData[0].strQuestion);
				objThis.loadQuestion();
				objThis.createEvents();

			}
		}
		objSegmentXML.load("xml/segment_11.xml");
	}

	private function createEvents():Void
	{
		var objThis = this;
		// Option Events
		var nLength = m_objCurrentQuestion.arrOptions[0].length;
		for(var i:Number = 0; i < nLength ; i++)
		{
			var mcOption = m_mcContainer["mcOption" + (i+1)];
			mcOption.mcBase.onRollOver = function()
			{
				if(!this._parent.selected)
				{
					this.gotoAndPlay("rollover");
				}
			}

			mcOption.mcBase.onRollOut = mcOption.mcBase.onReleaseOutside = function()
			{
				if(!this._parent.selected)
				{
					this.gotoAndPlay("rollout");
				}
			}

			mcOption.mcBase.onRelease = function()
			{
				objThis.m_mcContainer.gotoAndPlay("removeQuestion");
				objThis.deselectAllOptions();
				this._parent.selected = true;
				this.gotoAndPlay("selected");
				objThis.checkAnswer(this._parent.index);
				objThis.disableAllOptions();
			}
			mcOption.mcBase.enabled = false;
		}
		m_objCuePointListner = new Object();

		m_objCuePointListner.cuePoint = function(eventObject:Object):Void
		{

			trace("eventObject.info.name == " + eventObject.info.name);
			if(eventObject.info.name == "show_score")
			{
				objThis.disableAllOptions();
				objThis.m_mcContainer.mcScore._visible = true;
			}
			else if(eventObject.info.name == "hide_score")
			{
				objThis.m_mcContainer.mcScore._visible = false;
			}
			else if(eventObject.info.name == "a")
			{
				objThis.m_mcContainer.gotoAndPlay("optionA");
			}
			else if(eventObject.info.name == "b")
			{
				objThis.m_mcContainer.gotoAndPlay("optionB");
			}
			else if(eventObject.info.name == "c")
			{
				objThis.m_mcContainer.gotoAndPlay("optionC");
			}
			else if(eventObject.info.name == "d")
			{
				objThis.m_mcContainer.gotoAndPlay("optionD");
			}
			else if(eventObject.info.name == "pause")
			{

				_root.flvPlayer.pause();
				objThis.enableAllOptions();
			}
			else if(eventObject.info.name == "next_question_pos")
			{
				_root.flvPlayer.pause();
				objThis.onQuestionFinished();

				//objThis.loadNextScreen();
			}
			else if(eventObject.info.name == "next_question_neg")
			{
				_root.flvPlayer.pause();
				objThis.onQuestionFinished();
				//objThis.loadNextScreen();
			}
		};

		m_objCuePointListner.complete = function(eventObject:Object):Void
		{
			objThis.showResult()
		}

		_root.flvPlayer.addEventListener("cuePoint",m_objCuePointListner);
		_root.flvPlayer.addEventListener("complete",m_objCuePointListner);

		m_mcContainer.mcTimeOut.mcContinue.mcBase.onRollOver = function()
		{
			this.gotoAndPlay("rollover");
		}
		m_mcContainer.mcTimeOut.mcContinue.mcBase.onRollOver = function()
		{
			this.gotoAndPlay("rollout");
		}

		m_mcContainer.mcTimeOut.mcContinue.mcBase.onRelease = function()
		{
			objThis.onQuestionFinished();
		}
		// Event Listener
		//m_mcContainer.mcQuestion.mcTimer.addEventListener("onTimeOut", loadNextScreen)


		m_mcContainer.mcScoreBoard.mcPrint.mcPrint.btnPrint.onRollOver = function()
		{
			this._parent.play();
		}

		m_mcContainer.mcScoreBoard.mcPrint.mcPrint.btnPrint.onRollOut = function()
		{
			this._parent.play();
		}

		m_mcContainer.mcScoreBoard.mcPrint.mcPrint.btnPrint.onRelease = function()
		{
			print(this._parent._parent._parent, "bframe");
		}

		m_mcContainer.mcScoreBoard.mcCoutinue.mcCoutinue.btnCoutinue.onRollOver = function()
		{
			this._parent.play();
		}

		m_mcContainer.mcScoreBoard.mcCoutinue.mcCoutinue.btnCoutinue.onRollOut = function()
		{
			this._parent.play();
		}

		m_mcContainer.mcScoreBoard.mcCoutinue.mcCoutinue.btnCoutinue.onRelease = function()
		{
			objThis.nextSegment();
		}
	}

	private function nextSegment()
	{
		_root.objUpFront.segmentIndex++;
		//todo
	}

	private function deselectAllOptions():Void
	{
		for(var i:Number = 0; i<m_objCurrentQuestion.arrOptions.length; i++)
		{
			var mcOption = m_mcContainer["mcOption" + (i+1)];
			mcOption.index = i;
			mcOption.selected = false;
			mcOption.mcBase.gotoAndStop("rollover");
		}
	}

	private function disableAllOptions():Void
	{
		for(var i:Number = 0; i<m_objCurrentQuestion.arrOptions.length; i++)
		{
			var mcOption = m_mcContainer["mcOption" + (i+1)];
			mcOption.mcBase.enabled = false;
			mcOption.mcBase.gotoAndStop("rollover");
		}
	}

	private function enableAllOptions():Void
	{
		for(var i:Number = 0; i<m_objCurrentQuestion.arrOptions.length; i++)
		{
			var mcOption = m_mcContainer["mcOption" + (i+1)];
			mcOption.mcBase.enabled = true;
		}
	}

	private function loadQuestion()
	{
		m_objCurrentQuestion = m_arrQuestionsData[m_intCurrentQuestion];
		m_mcContainer.mcQuestion.txtQuestion.htmlText = m_objCurrentQuestion.strQuestion;
		//set option text
		for(var i:Number = 0; i<m_objCurrentQuestion.arrOptions.length; i++)
		{
			var mcOption = m_mcContainer["mcOption" + (i+1)];
			mcOption.txtOption.htmlText = m_objCurrentQuestion.arrOptions[i];
		}
		_root.flvPlayer.contentPath =  m_objCurrentQuestion.strFlvPath;
		_root.flvPlayer.seek(0);
		_root.flvPlayer.play();
		//m_mcContainer.gotoAndPlay("question");
	}

	private function checkAnswer(intOptionNumber:Number):Void
	{
		m_mcContainer.mcQuestion.mcTimer.stopTimer();
		m_isCorrect = (intOptionNumber == m_objCurrentQuestion.intCorrectOption);
	}

	public function showFeedback()
	{

		if(m_isCorrect)
		{
			// Correct
			_root.flvPlayer.seekToNavCuePoint("positive");
			_root.flvPlayer.play();
			m_intTotalCorrect++
			m_mcContainer.mcScore.txtLabel.text = m_intTotalCorrect;
		}
		else
		{
			// Wrong
			_root.flvPlayer.seekToNavCuePoint("negative");
			_root.flvPlayer.play();
		}
		m_mcContainer.mcScore._visible = true;
	}

	public function loadNextScreen():Void
	{
		deselectAllOptions();
		m_mcContainer.mcQuestion.mcTimer.resetTimer();
		m_intCurrentQuestion++;
		trace(m_intCurrentQuestion +  "  m_arrQuestionsData.length = " + m_arrQuestionsData.length);
		if(m_intCurrentQuestion < m_arrQuestionsData.length)
		{
			loadQuestion();
		}
		else
		{
			trace("m_strCongrVideoPath   == " + m_strCongrVideoPath);
			_root.flvPlayer.contentPath =  m_strCongrVideoPath;
			_root.flvPlayer.seek(0);
			_root.flvPlayer.play();
		}
	}

	public function showResult()
	{
		var strValue = "Well done <FirstName> <LastName> you have complited the UpFront Quiz. Please print out this certificate and bring it to the face to face course.<br><br> You Scored: " + m_intTotalCorrect + " out of " + m_arrQuestionsData.length;;
		m_mcContainer.mcScoreBoard.mcCongratulation.txtScore.html = true;
		m_mcContainer.mcScoreBoard.mcCongratulation.txtScore.htmlText = strValue;
		m_mcContainer.mcScoreBoard._visible = true;
		m_mcContainer.mcScoreBoard.gotoAndPlay(2);
		trace(m_mcContainer.mcScoreBoard  +  "  ============---------------==============  "  +  m_mcContainer.mcScoreBoard._currentframe);
	}

	public function onQuestionFinished()
	{
		m_mcContainer.mcScore._visible = false;
		m_mcContainer.mcTimeOut._visible = false;
		_root.flvPlayer.stop();
		m_mcContainer.gotoAndStop("strt");
		loadNextScreen();
	}
}